package service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;
import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import mapper.WmMaterialMapper;
import mapper.WmNewsMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import service.WmMaterialService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fdfs.url}")
    private String fileURL;
    @Resource
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public ResponseResult fileUpload(MultipartFile multipartFile) {
        //.准备工作
        if(multipartFile==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1.上传图片到fastdfs
        String fileId = null;
        fileId = fastDFSClient.uploadFile(multipartFile);
        //2.保存数据库表
        WmMaterial wm = new WmMaterial();
        wm.setCreatedTime(new Date());
        //是否收藏
        wm.setIsCollection(0);
        //图片的类型 1是视频  0是图片
        wm.setType(0);
        //从ThreadLocal中获取数据
        WmUser user = WmThreadLocalUtils.getUser();
        wm.setUserId(user.getApUserId());
        wm.setUrl(fileURL+fileId);
        //保存
        save(wm);
        return ResponseResult.okResult(wm);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检查参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.设置条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
//        //获取userId
        Integer userId = WmThreadLocalUtils.getUser().getApUserId();
        queryWrapper.eq(WmMaterial::getUserId,userId);
        queryWrapper.eq(dto.getIsCollection()!=null, WmMaterial::getIsCollection,dto.getIsCollection());
        Page pageInfo=new Page(dto.getPage(),dto.getSize());
        IPage page = page(pageInfo, queryWrapper);
        //设置封装数据
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public ResponseResult delById(Integer id) {
        //判定参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判定素材跟新闻是否有关联
        //根据id去素材关联表中查询数据
        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNewsMaterial::getMaterialId,id);
        //查询记录数
        Integer count = wmNewsMaterialMapper.selectCount(queryWrapper);
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"素材跟新闻有关联");
        }
        //删除fastdfs的内容
        //查询图片的路径
        WmMaterial material = getById(id);
        fastDFSClient.delFile(material.getUrl());
        //删除素材表中的记录
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult setIsCollection(Integer id, Integer type) {
        //判定参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置局部修改的字段
        LambdaUpdateWrapper<WmMaterial> updateWrapper =new LambdaUpdateWrapper<>();
        updateWrapper.set(WmMaterial::getIsCollection,type);
        updateWrapper.eq(WmMaterial::getId,id);
        //修改
        update(updateWrapper);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
